<template>
  <div>
    <el-table
      v-loading="subLoading"
      :show-header=false
      :data="bannerData"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        label="图片"
        prop="url"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-image @click="bigImg(`${scope.row.url}`)"
            style="width: 300px; height: 120px; margin-bottom:-4px"
            :src="scope.row.url"
            :preview-src-list="srcList"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="60"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button
      type="info"
      plain
      icon="el-icon-upload"
      size="mini"
      @click="handleImport"
    >新增</el-button>

    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".jpg, .png, .webp"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-error="handleUploadError"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <p>仅允许导入jpg、png、bmp格式的图片文件</p>
          <p>大小不超过 <b style="color: #f56c6c">1MB,建议尺寸1920*650</b></p>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { removeIndexBanner } from "@/api/content/html";
import { getToken } from "@/utils/auth";

export default {
  props: {
    htmlcontent: {
      type: Object,
    }
  },
  data() {
    return {
      bannerData: [],
      srcList:["https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg"],
      loading: true,
      subLoading: false,
      previews: {},
      resizeHandler: null,
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/html/content/uploadIndexBanner"
      },
    };
  },
  watch: {
    htmlcontent: 'init',
  },
  methods: {
    init() {
      this.bannerData = [];
      let banners = this.htmlcontent.indexBanners;
      for (let i = 0; i < banners.length; i++) {
        const banner = {};
        banner['banner'] = banners[i];
        banner['url'] = process.env.VUE_APP_BASE_API + banners[i];
        this.bannerData.push(banner);
      }
    },
    handleImport() {
      this.upload.title = "首页banner图上传";
      this.upload.open = true;
    },
    // 上传失败
    handleUploadError(err) {
      this.$modal.msgError("上传文件失败，请重试");
      this.$modal.closeLoading();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.$emit('saveBanner');
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "修改结果", { dangerouslyUseHTMLString: true });
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    handleDelete(data) {
      this.$modal.confirm('是否确认删除？').then(() => {
        removeIndexBanner(data).then(() => {
          this.$emit('saveBanner');
          this.$modal.msgSuccess('删除成功');
        }).catch(() => {});
      })
    },
    bigImg(url) {
      this.srcList[0] = url;
    },
  }
};
</script>
<style scoped lang="scss">

</style>
